local yaoxing = fk.CreateSkill {
  name = "3s__yaoxing",
    dynamic_desc = function(self, player)
    local texts = {"3s__yaoxing_inner", "3s__yaoxing_show", "3s__yaoxing_1", "3s__yaoxing_recast", "3s__yaoxing_2", "3s__yaoxing_give", "3s__yaoxing_tail"}
    if table.contains(player:getTableMark("yaoxing_edit"), "show") then
      texts[2] = "3s__yaoxing_discard"
    end
    if table.contains(player:getTableMark("yaoxing_edit"), "recast") then
      texts[4] = "3s__yaoxing_discard"
    end
    if table.contains(player:getTableMark("yaoxing_edit"), "give") then
      texts[6] = "3s__yaoxing_discard"
    end
    return table.concat(texts, ":")
  end,
}

Fk:loadTranslationTable{
  ["3s__yaoxing"] = "邀幸",
  [":3s__yaoxing"] = "出牌阶段限一次，你可以令一名其他角色{展示}你一张手牌，令你此阶段可执行一次：{重铸}一张牌以视为使用被选择的手牌，然后{交给}其一张牌。若上述三张牌花色相同，你复原一个{}的内容并重置此技能。",
  [":3s__yaoxing_inner"] = "出牌阶段限一次，你可以令一名其他角色{1}{2}{3}{4}{5}{6}",
  ["3s__yaoxing_1"] = "你一张手牌，令你此阶段可执行一次：",
  ["3s__yaoxing_2"] = "一张牌以视为使用展示牌，然后",
  ["3s__yaoxing_tail"] = "其一张牌。若上述三张牌花色相同，你复原一个{}的内容并重置此技能。",
  ["3s__yaoxing_show"] = "<font color='red'><b>{展示}</b></font>",
  ["3s__yaoxing_recast"] = "<font color='red'><b>{重铸}</b></font>",
  ["3s__yaoxing_give"] = "<font color='red'><b>{交给}</b></font>",
  ["3s__yaoxing_discard"] = "<font color='grey'><b>{弃置}</b></font>",
  ["#3s__yaoxing-trigger"] = "邀幸：你可以令%src展示你一张手牌，令你此阶段可使用、打出或弃置该手牌。",
  ["3s__yaoxing1"] = "令一名其他角色展示你一张手牌",
  ["3s__yaoxing2"] = "重铸一张牌以视为使用展示牌",
  ["#yaoxing-choose"] = "邀幸：请选择%dest展示的手牌",
  ["#3s__yaoxing-use"] = "邀幸：请视为使用牌",
  ["#3s__yaoxing-throw"] = "邀幸：请弃置%dest一张牌",
  ["@yaoxing-phase"] = "邀幸",
  ["@@3s__yaoxing-phase"] = "邀幸",
  ["#3s__yaoxing-ask"] = "邀幸：请选择交给%dest的牌",
}

yaoxing:addEffect("active", {
  prompt = "邀幸：请选择一项执行",
  min_target_num = 0,
  max_target_num = 1,
  min_card_num = 0,
  max_card_num = 1,
  interaction = function(self, player)
    local all_choices = {"3s__yaoxing1", "3s__yaoxing2"}
    local choices = {}
    if player:usedSkillTimes(yaoxing.name, Player.HistoryPhase) == 0 then
      table.insert(choices, "3s__yaoxing1")
    end
    if player:getMark("yaoxing-phase") ~= 0 then
      table.insert(choices, "3s__yaoxing2")
    end
    return UI.ComboBox { choices = choices, all_choices = all_choices }
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(yaoxing.name, Player.HistoryPhase) == 0 or
    (player:getMark("yaoxing-phase") ~= 0 and player:getMark("yaoxing_used-phase") == 0)
  end,
  card_filter = function(self, player, to_select, selected)
    if self.interaction.data == "3s__yaoxing1" then
      return false
    elseif self.interaction.data == "3s__yaoxing2" then
      return #selected == 0
    end
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    if self.interaction.data == "3s__yaoxing1" then
      return to_select ~= player and #selected == 0
    elseif self.interaction.data == "3s__yaoxing2" then
      return false
    end
  end,
  feasible = function(self, player, selected, selected_cards, card)
    if self.interaction.data == "3s__yaoxing1" then
      return #selected == 1 and #selected_cards == 0
    elseif self.interaction.data == "3s__yaoxing2" then
      return #selected == 0 and #selected_cards == 1
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    if self.interaction.data == "3s__yaoxing1" then
       local to = effect.tos[1]
        local toshow = room:askToChooseCards(to, {
            target = player,
            flag = "h",
            min = 1,
            max = 1,
            skill_name = yaoxing.name,
            prompt = "#yaoxing-choose::" .. to.id
        })
        if #toshow > 0 then
          room:setPlayerMark(player, "yaoxing-suit-phase", Fk:getCardById(toshow[1]):getSuitString())
          room:addTableMarkIfNeed(player, "@yaoxing-phase", Fk:getCardById(toshow[1]).name)
          if table.contains(player:getTableMark("yaoxing_edit"), "show") then
            room:throwCard(toshow, yaoxing.name, player, to)
          else
            player:showCards(toshow)
            room:setCardMark(Fk:getCardById(toshow[1]), "@@3s__yaoxing-phase", 1)
          end
          room:setPlayerMark(player, "yaoxing-phase", to.id)
        end
    elseif self.interaction.data == "3s__yaoxing2" then
        room:addPlayerMark(player, "yaoxing_used-phase")
        if table.contains(player:getTableMark("yaoxing_edit"), "recast") then
          room:throwCard(effect.cards, yaoxing.name, player, player) 
        else
          room:recastCard(effect.cards, player)
        end
        local pattern = player:getTableMark("@yaoxing-phase")
        room:askToUseVirtualCard(player, {
          name = pattern,
          cancelable = false,
          extra_data = { bypass_times = true },
          prompt = "#3s__yaoxing-use",
        })
        local to_id = player:getMark("yaoxing-phase")
        local to = room:getPlayerById(to_id)
        local card_id
        if table.contains(player:getTableMark("yaoxing_edit"), "give") then
          if not to:isNude() then
            local to_throw = room:askToChooseCards(player, {
              target = to,
              flag = "he",
              min = 1,
              max = 1,
              skill_name = yaoxing.name,
              prompt = "#3s__yaoxing-throw::"..to_id
            })
            card_id = to_throw[1]
            room:throwCard(to_throw, yaoxing.name, to, player)
          end
        elseif not player:isNude() then
          local to_give = room:askToCards(player, {
              min_num = 1,
              max_num = 1,
              skill_name = yaoxing.name,
              pattern = ".",
              prompt = "#3s__yaoxing-ask::"..to_id,
              cancelable = false,
          })
          if #to_give > 0 then
            card_id = to_give[1]
            room:obtainCard(to, to_give[1], false, fk.ReasonGive, to, yaoxing.name)
          end
          room:setPlayerMark(player, "yaoxing-phase", 0)
        end
        if Fk:getCardById(card_id):getSuitString() == Fk:getCardById(effect.cards[1]):getSuitString()
        and Fk:getCardById(card_id):getSuitString() == player:getMark("yaoxing-suit-phase") then
          player:setSkillUseHistory(yaoxing.name, 0, Player.HistoryPhase)
          room:setPlayerMark(player, "yaoxing_used-phase", 0)
          room:setPlayerMark(player, "@yaoxing-phase", 0)
          local choices = player:getTableMark("yaoxing_edit")
          if #choices > 0 then
            local choice = room:askToChoice(player, {
              choices = choices,
              skill_name = yaoxing.name,
              all_choices = {"show", "recast", "give"},
            })
            room:removeTableMark(player, "yaoxing_edit", choice)
          end
        end
    end
  end
})

return yaoxing